/**
 * @class
 * @memberof w.parser
 * @param {*} message 消息
 * @param {*} callback 处理完成的回调
 */
async function handleImage(message, callback) {
  let { imageData,imageType,bigTextureQuality } = message;
  // 将 Uint8Array 转换为 Blob
  const blob = new Blob([imageData], { type: imageType });
  // 创建 ImageBitmap
  const bitmap = await createImageBitmap(blob);
  if (bitmap.width > 8192 || bitmap.height > 8192) {
    let scale = Math.max(bitmap.width, bitmap.height) / 8192;
    const width = Math.ceil(bitmap.width / scale);
    const height = Math.ceil(bitmap.height / scale);
    const data = await compressImageSize(bitmap, width, height);
    callback({ data: { bitmap :data}, transferlist: [data] });
  }else {
    if (bigTextureQuality !== 1) {
      if (bitmap.width > 2048 || bitmap.height > 2048) {
       const width = Math.ceil(bitmap.width * bigTextureQuality);
       const height = Math.ceil(bitmap.height * bigTextureQuality);
       const data = await compressImageSize(bitmap, width, height);
        callback({ data: { bitmap:data }, transferlist: [data] });
      } else {
        callback({ data: { bitmap }, transferlist: [bitmap] });
      }
    } else {
      callback({ data: { bitmap }, transferlist: [bitmap] });
    }
  }
 
}
async function compressImageSize(img, maxWidth, maxHeight) {
// 创建 OffscreenCanvas  
const offscreenCanvas = new OffscreenCanvas(maxWidth, maxHeight);  
const ctx = offscreenCanvas.getContext('2d');  
// 将图像绘制到 OffscreenCanvas  
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, offscreenCanvas.width, offscreenCanvas.height);  
// 创建 ImageBitmap  
const bitmap = await createImageBitmap(offscreenCanvas); 
 return bitmap
}
export { handleImage }
